﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace YuFeng.Web.Areas.Admin.Controllers
{
    public class BaseController : Controller
    {
        #region My Action Results
        protected ActionResult ProcessFormatedResult(bool result, string message, string actionName = "", object model = null, object routeValues = null)
        {
            if (result)
            {
                if (Request.IsAjaxRequest())
                    return Json(new { Result = true, Message = message, Url = Url.Action(actionName, routeValues) });
                else
                    return RedirectToAction(actionName, routeValues);
            }
            else
            {
                if (Request.IsAjaxRequest())
                    return Json(new { Result = false, Message = message });
                else
                    if (model != null)
                        return View(model);
                    else
                        return View();
            }
        }
        protected string GetReturnUrl(string returnUrl, string defaultUrl)
        {
            if (!String.IsNullOrEmpty(returnUrl))
                return returnUrl;
            if (Request.UrlReferrer != null
                && Request.Url.AbsoluteUri != Request.UrlReferrer.AbsoluteUri)
                return Request.UrlReferrer.AbsoluteUri;
            return defaultUrl;
        }
        #endregion
        #region 公共方法
        protected PartialViewResult DropDownList(string name, IEnumerable<SelectListItem> list, string optionLabel, string optionValue, object htmlAttributes = null)
        {
            ViewBag.Name = name;
            if (String.IsNullOrEmpty(optionLabel))
                optionLabel = "请选择...";
            var tmpList = list.ToList();
            tmpList.Insert(0, new SelectListItem
            {
                Value = optionValue,
                Text = optionLabel,
                Selected = !list.Any(o => o.Selected)
            });
            list = tmpList.AsEnumerable();
            ViewBag.KVList = list;
            ViewBag.HtmlAttributes = htmlAttributes;
            return PartialView("DropDownList");
        }
        #endregion

        /// <summary>
        /// 返回错误消息
        /// </summary>
        /// <param name="errmsg"></param>        
        /// <returns></returns>
        protected JsonResult ErrorMsg(string errmsg)
        {
            return this.ErrorMsg<string>(errmsg, null);
        }

        /// <summary>
        /// 返回错误消息
        /// </summary>
        /// <param name="errmsg"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        protected JsonResult ErrorMsg<T>(string errmsg, T data)
        {
            return Json(new
            {
                status = false,
                errmsg = errmsg,
                data = data
            }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 返回成功消息
        /// </summary>                
        /// <returns></returns>
        protected JsonResult SuccessMsg()
        {
            return this.SuccessMsg<string>(null);
        }

        /// <summary>
        /// 返回成功消息
        /// </summary>        
        /// <param name="data">返回的数据</param>
        /// <returns></returns>
        protected JsonResult SuccessMsg<T>(string errmsg, T data)
        {
            return Json(new
            {
                status = true,
                errmsg = errmsg,
                data = data
            }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 返回成功消息
        /// </summary>        
        /// <param name="data">返回的数据</param>
        /// <returns></returns>
        protected JsonResult SuccessMsg<T>(T data)
        {
            return Json(new
            {
                status = true,
                errmsg = "ok",
                data = data
            }, JsonRequestBehavior.AllowGet);
        }
        #region 分页信息

        /// <summary>
        /// 默认当前页面是第一页
        /// </summary>
        public int PageIndex
        {
            get
            {
                string pageindex = System.Web.HttpContext.Current.Request.QueryString["pageindex"];
                int p;
                if (!int.TryParse(pageindex, out p))
                {
                    p = 1;
                }

                return p;
            }
        }
        /// <summary>
        /// 默认每页显示5条信息
        /// </summary>
        public virtual int PageSize
        {
            get
            {
                string pagesize = System.Web.HttpContext.Current.Request.QueryString["pagesize"];
                int p;
                if (!int.TryParse(pagesize, out p))
                {
                    p = 10;
                }

                return p;
            }
        }

        public string Order
        {
            get
            {
                string order = System.Web.HttpContext.Current.Request.QueryString["order"];
                return string.IsNullOrWhiteSpace(order) ? "ID" : order;
            }
        }
        public string By
        {
            get
            {
                string by = System.Web.HttpContext.Current.Request.QueryString["by"];
                return string.IsNullOrWhiteSpace(by) ? "DESC" : by;
            }
        }
        #endregion
    }
}